package com.example.administrator.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.administrator.adapter.PreviewAdapter;
import com.example.administrator.bean.AlbumImage;
import com.example.administrator.impl.OnCompatCompoundCheckListener;
import com.example.administrator.mypictureshow.R;

import java.util.List;

/**
 * 预览图片选择Dialog
 * Created by chenfeifei on 2018\8\1 0001.
 */

public class PreviewDialog extends Dialog {

    private Context mContext;
    private List<AlbumImage> mAlbumImages;//预览显示的图片
    ViewPager mViewPager;
    Button preview_finish;
    TextView number_info;
    CheckBox current_isCheck;
    //复选框按钮显示
    private OnCompatCompoundCheckListener mCheckListener;
    //当前选中position
    private int mCheckedImagePosition;


    public PreviewDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public PreviewDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }
    //设置预览图数据
    public void setPreviewData(List<AlbumImage> mAlbumImages,OnCompatCompoundCheckListener checkListener){
        this.mAlbumImages = mAlbumImages;
        this.mCheckListener = checkListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View dialog_content = LayoutInflater.from(mContext).inflate(R.layout.dialog_photo_preview,null);
        setContentView(dialog_content);
        setDialogSize();
        //进行Dialog内容设置
        mViewPager = (ViewPager) dialog_content.findViewById(R.id.view_pager);
        preview_finish = (Button) dialog_content.findViewById(R.id.preview_finish);
        number_info = (TextView) dialog_content.findViewById(R.id.number_info);
        current_isCheck = (CheckBox) dialog_content.findViewById(R.id.current_isCheck);

        initializeViewPager(0);
        number_info.setText(1+"/"+mAlbumImages.size());
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                number_info.setText(position + 1 + "/" + mAlbumImages.size());
            }
        });
        preview_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private void initializeViewPager(int currentItem) {
        if (mAlbumImages.size() > 2){
            mViewPager.setOffscreenPageLimit(2);
        }
        PreviewAdapter previewAdapter = new PreviewAdapter(mAlbumImages, getScreenH(mContext) - getStatusBarHeight(mContext));
        mViewPager.setAdapter(previewAdapter);
        ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mCheckedImagePosition = position;
                AlbumImage albumImage = mAlbumImages.get(mCheckedImagePosition);
                current_isCheck.setChecked(albumImage.isChecked());
            }
        };
        mViewPager.addOnPageChangeListener(pageChangeListener);
        pageChangeListener.onPageSelected(0);
        mViewPager.setCurrentItem(currentItem);

        current_isCheck.setChecked(true);
        current_isCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(!isChecked){

                }
                mCheckListener.onCheck(buttonView,mCheckedImagePosition,isChecked);
            }
        });
    }


    private void setDialogSize(){
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = getScreenWidth();
        layoutParams.height = getScreenH(mContext) - getStatusBarHeight(mContext);
        layoutParams.gravity = Gravity.BOTTOM;
        window.setAttributes(layoutParams);
        window.setWindowAnimations(R.style.bottomDialogAnim);
    }

    private int getScreenWidth() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * 获取高度
     * @param context
     * @return
     */
    public int getScreenH(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.heightPixels;
        return screenWidth;
    }

    private int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
